

$(document).ready(function(){

	LoadSortbyClass();

	$("#sort_review_count").click(
		function(){
			SetSortBy( "review_count" );
			return false;
		}
	);
	
	$("#sort_price").click(
		function(){
			SetSortBy( "price" );
			return false;
		}
	);
	
	$("#sort_create_time").click(
		function(){
			SetSortBy( "create_time" );
			return false;
		}
	);
	
	$("#sort_asc").click(
		function(){
			SetSortOrder( "asc" );
			return false;
		}
	);
	
	$("#sort_desc").click(
		function(){
			SetSortOrder( "desc" );
			return false;
		}
	);

	var link = window.location.toString();
	if( $("#sortby").val() != "" || $("#q").val() != "" || link.indexOf("p=") != -1
		&& link.indexOf( "sortanchor" ) == -1 ){
			window.location = window.location + "#sortanchor";
	}
});

$(document).ready(function(){ 

   	$(".goodlist > dt span").toggle(function(){
				
		$(".goodlist").addClass("expanded"); 

	},function(){
		$(".goodlist").removeClass("expanded"); 

	});   	
});


function SetSortBy( type ){
	//reset the sort by selector

	$( "#sortby" ).val( type+"-asc" );
	$( "#filterform" ).submit();
}

function SetSortOrder( order ){
	//reset the sort by selector
	var sortby = $( "#sortby" ).val();
	
	var num=sortby.indexOf("-");

	var type = sortby.substr(0,num);

	
	$( "#sortby" ).val( type+'-'+order );
	
	$( "#filterform" ).submit();
}


function LoadSortbyClass(){
	var sortby = $( "#sortby" ).val();

	if( sortby.indexOf( "review_count" ) >= 0 ){
	
		$( "#sort_review_count" ).replaceWith('<span>'+$( "#sort_review_count" ).text()+'</span>');
	}

	if( sortby.indexOf( "create_time" ) >= 0 ){
	
		$( "#sort_create_time" ).replaceWith('<span>'+$( "#sort_create_time" ).text()+'</span>');
	}

	if( sortby.indexOf( "price" ) >= 0 ){
	
		$( "#sort_price" ).replaceWith('<span>'+$( "#sort_price" ).text()+'</span>');
	}

}